package com.ffly.astar;

public class NodePath implements Comparable<NodePath> {
	public NodePath parent;//上一个节点
	public int g;//当前点到终点的距离
	public int h;//权重
	public int x;
	public int y;

	public NodePath() {

	}

	public NodePath(NodePath parent, int g, int h, int x, int y) {
		this.parent = parent;
		this.g = g;
		this.h = h;
		this.x = x;
		this.y = y;
	}

	public static NodePath create(NodePath parent, int dir, int targetX,
			int targetY) {
		int x = 0,y = 0;
		switch (dir) {
		case 0:
			x = parent.x;
			y = parent.y - 1;
			break;
		case 1:
			x = parent.x + 1;
			y = parent.y - 1;
			break;
		case 2:
			x = parent.x + 1;
			y = parent.y;
			break;
		case 3:
			x = parent.x + 1;
			y = parent.y + 1;
			break;
		case 4:
			x = parent.x;
			y = parent.y + 1;
			break;
		case 5:
			x = parent.x - 1;
			y = parent.y + 1;
			break;
		case 6:
			x = parent.x - 1;
			y = parent.y;
			break;
		case 7:
			x = parent.x - 1;
			y = parent.y - 1;
			break;
		default:
			break;
		}
		
		int x1 = x - targetX;
		int y1 = y - targetY;
		int g = (int) Math.sqrt(x1 * x1 + y1 * y1);
		int h = Math.abs(x1) + Math.abs(y1);

		return new NodePath(parent, g, h, x, y);
	}

	public int getF() {
		return g + h;
	}

	@Override
	public int compareTo(NodePath o) {
		if (getF() > o.getF()) {
			return -1;
		}

		if (getF() < o.getF()) {
			return 1;
		}

		return 0;
	}
	
	public String getXYString(){
		return x+""+y;
	}
	
	@Override
	public String toString() {
		return "{x:"+x+" y:"+y+" g:"+g+" h:"+h+" f:"+getF()+"}";
	}
}
